﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLayer;
using ModelLayer.ObraDesligadaTableAdapters;
using ModelLayer;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Xml;
using System.IO;

namespace PresentationLayer
{
    public partial class Form1 : Form
    {
        private ApplicationContext af;

        public Form1(ApplicationContext af)
        {
            this.af = af;
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            AdicionarActo.Enabled = CriarObra.Enabled = ApplicationData.Profissao_Utilizador.Equals("recepcionista", StringComparison.InvariantCultureIgnoreCase);
        }

        private void CriarObra_Click(object sender, EventArgs e)
        {
            CriarObra obra = new CriarObra();
            obra.ShowDialog();
        }

        private void AlterarObra_Click(object sender, EventArgs e)
        {
            new AlterarObra().ShowDialog();
        }

        private void CriarCliente_Click(object sender, EventArgs e)
        {
            CriarCliente cliente = new CriarCliente();
            cliente.ShowDialog();
        }

        private void AlterarCliente_Click(object sender, EventArgs e)
        {

        }

        private void sair_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void TerminarSessao_Click(object sender, EventArgs e)
        {
            Login log = new Login(this.af);
            this.af.MainForm = log;
            this.af.MainForm.Show();
            this.Close();
        }

        private void AdicionarVeiculo_Click(object sender, EventArgs e)
        {
            new CriarVeiculo().ShowDialog();
        }

        private void AdicionarFuncionario_Click(object sender, EventArgs e)
        {
            new CriaFuncionario().ShowDialog();
        }

        private void NovaOficina_Click(object sender, EventArgs e)
        {
            new CriaOficina().ShowDialog();
        }

        private void AdicionarActo_Click(object sender, EventArgs e)
        {
            new CriaActo().ShowDialog();
        }

        private void LançarFactura_Click(object sender, EventArgs e)
        {
            new EmiteFactura().ShowDialog();
        }

        private void CriarDep_Click(object sender, EventArgs e)
        {
            new CriarDepartamento().ShowDialog();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            System.Drawing.Icon img = System.Drawing.Icon.ExtractAssociatedIcon(@"Motor.ico");
            this.Icon = new System.Drawing.Icon(img, img.Size);
        }

        private void AlterarOficina_Click(object sender, EventArgs e)
        {
            new AlterarOfic().ShowDialog();
        }

        private void PorConcluir_Click(object sender, EventArgs e)
        {
            this.richTextListas.AppendText("Obra Estado Acto Oficina Departamento Designaçao HorasEstimadas");

            foreach (String str in ObraActions.ListarObrasPorConcluir())
            {
                this.richTextListas.AppendText("\n");
                this.richTextListas.AppendText(str);
            }

        }

        private void AddPeca_Click(object sender, EventArgs e)
        {
            new AddPeca().ShowDialog();
        }

        private void ActuPreco_Click(object sender, EventArgs e)
        {
           new ActuPrecoPeca().ShowDialog();
        }
        private void richTextListas_TextChanged(object sender, EventArgs e)
        {
            new AdicionaActoObra().ShowDialog();
        }

        private void listaPeça_Click(object sender, EventArgs e)
        {
            this.richTextListas.ResetText();
            this.richTextListas.AppendText("Referência Desiganção Preço Numero de Obras");
            foreach (String str in PecaActions.ListaDePecas()) {
                this.richTextListas.AppendText("\n");
                this.richTextListas.AppendText(str);
            }
        }
        private void TerminActo_Click(object sender, EventArgs e)
        {
            TermiActoObra termObra = new TermiActoObra();
            termObra.ShowDialog();
        }

        private void PagarFactura_Click(object sender, EventArgs e)
        {
            PagarFact pgFct = new PagarFact();
            pgFct.ShowDialog();
        }

        private void adicionarDepartamentoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AdicDepartamentoFunc().ShowDialog();
        }

        private void criarProfissao_Click(object sender, EventArgs e)
        {
            new CriarProfissao().ShowDialog();
        }

        private void associarProfissaoDepartamento_Click(object sender, EventArgs e)
        {
            new AssociarProfADep().ShowDialog();
        }

        private void AdicActoObra_Click(object sender, EventArgs e)
        {
            new AdicionaActoObra().ShowDialog();
        }

        private void adicionarResponsavelOfic_Click(object sender, EventArgs e)
        {
            new AdicionarResponsavelOficina().ShowDialog();
        }

        private void adicionarResponsavelDep_Click(object sender, EventArgs e)
        {
           new AdicionarResponsavelDep().ShowDialog();
        }

        private void HabilitarFuncionario_Click(object sender, EventArgs e)
        {
            new HabilitarFuncionario().ShowDialog(); 
        }

        private void ModoDesl_Click(object sender, EventArgs e)
        {
            new ModoDesligadoFrom().ShowDialog();
           
        }

        private void ListaModoDesliga_Click(object sender, EventArgs e)
        {
            this.richTextListas.ResetText();
            DesligadosActions.ListaObrasDesligadas(ref this.richTextListas, ApplicationData.obra, ApplicationData.obraCont, ApplicationData.resr, ApplicationData.oficina);
        }

        private void adicionarCom_Click(object sender, EventArgs e)
        {
            new AdicioComun().ShowDialog();
        }

        private void transformarInfo_Click(object sender, EventArgs e)
        {
            String xm = OficinaActions.GetXml();
            if (xm != null)
            {
                XPathDocument xp = new XPathDocument(new StreamReader(OficinaActions.criaFicheiroXMl(xm)));
                XslCompiledTransform cp = new XslCompiledTransform();
                cp.Load("oficinas.xsl");
                SaveFileDialog sv = new SaveFileDialog();
                sv.Filter = "html files (*.html)|*.html";
                sv.FilterIndex = 1;
                sv.AddExtension = true;
                sv.ShowDialog();

                if (sv.FileName != "")
                {
                    XmlTextWriter writeHtmlOficnas = new XmlTextWriter(sv.FileName, null);
                    cp.Transform(xp, writeHtmlOficnas);
                    writeHtmlOficnas.Close();
                }
            }
        }

        private void comunicadosXML_Click(object sender, EventArgs e)
        {
            String xml = ComunicadoActions.TodosComunicados();
            SaveFileDialog sv = new SaveFileDialog();
            sv.Filter = "xml files (*.xml)|*.xml";
            sv.FilterIndex = 1;
            sv.AddExtension = true;
            sv.ShowDialog();
            if (sv.FileName != "")
            {
                XmlTextWriter writeXML = new XmlTextWriter(sv.FileName, null);
                writeXML.WriteRaw(xml);
                writeXML.Close();
            }

        }
           

        private void Comuni911_Click(object sender, EventArgs e)
        {
            ComunicadoActions.ListaNoEcranComunicados911(ref this.richTextListas);
        }
       
          
    }
}
